Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java Immutable String

  • Any application program must contain a String as a type of variable. To store different attributes like usernames and passwords, etc., string references are used. String objects are immutable in Java. Immutable simply means that something cannot be altered or changed.

  • Once a String object is created, it cannot have its data or state changed; instead, a new String object must be created.







Example of Immutable String in java


Example of Immutable String in java
 // Java example of Immutable String
public class Main {	
    public static void main(String args[]){  		 
	  String str="Welcome ";  
	  s.concat("To India");  //The string is added at the end by the concat() method.
	  System.out.println(s); //it will print welcome instead of India due to Immutable String 
  } 
}




Output:

Welcome  
Welcome is not changed here, but a new object with Welcome To India is created. That is why String is referred to as immutable.


Example of String explicitly assign it to the reference variable


Example of String explicitly assign it to the reference variable in java
public class Main {	
    public static void main(String args[]){  		 
	  String str="Welcome";  
	  str= str.concat("To India");  //if we explicitly assign it to the reference variable
	  System.out.println(str);   //it will print Welcome To India
  } 
}



In this case, str points to the "Welcome To India" message. Please keep in mind that the Welcome object has not been changed.

Output:

Welcome To India 

Why String objects are immutable in Java

Since Java makes use of the String literal concept. Assume there are five reference variables, all of which point to the same thing, "Sachin." All reference variables will be impacted if one reference variable alters the value of the object. Because of this, String objects in Java are immutable.

Several String characteristics make String objects immutable.

  • ClassLoader : A String object is used as an argument by a Java ClassLoader. Consider the possibility that the value could change and the class that is supposed to be loaded could change if the String object is modifiable.String is immutable in order to prevent this kind of misunderstanding.



  • Thread Safe : Since the String object cannot be changed, we don't need to worry about the synchronization needed when using it in multiple threads.
  • Security : Immutable String objects load the appropriate class to prevent additional errors, as we have seen with class loading. As a result, the application program becomes more secure. Think about a payment software illustration. Because String objects are immutable, no outsider can change the username or password. The application program may become more secure as a result.
  • Heap Space : The immutability of String contributes to a reduction in the amount of heap memory used. The JVM checks to see if the value is already present in the String pool when we attempt to declare a new String object. The new object is given the same value if it already exists. Java can effectively utilize the heap space thanks to this feature.



Why String class cannot be override in Java

The String class's final status is due to the fact that its methods cannot be overridden by any other classes. In order to give the new String objects and the old String objects the same features.


Java Immutable Class Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.